﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XMK.Tadong.Biz;
using XMK.Tadong.StructuredData_IntoDb;


namespace XMK.Tadong.Web.Views.SJCJDB
{
    public partial class BasicDataTree : System.Web.UI.Page
    {
        private static int userId;  //用户id
        protected void Page_Load(object sender, EventArgs e)
        {
           // ReloadLogin(1, "2");
            if (this.Session["UserID"] != null)
            {
                userId = 111;// Convert.ToInt32(this.Session["UserID"].ToString());
            }
            if (!Page.IsPostBack)
            {
                initializeTables();
            }

        }

        /// <summary>
        /// 获得结构化数据表名对应关系
        /// </summary>
        private void initializeTables()
        {

            DataTable dt = new DataTable();

            StructuredData_IntoDb.Data.DBTree db = new StructuredData_IntoDb.Data.DBTree(Config.TadongDbProvider);

            try
            {
                //ds = data_Table.GetDataTable_UserID(userId);
                dt = db.GetInputTree();
                if (dt != null)
                {
                    tvwBasicData.Nodes.Clear();
                    InitTree(dt, tvwBasicData);
                }
                else
                {
                    StructuredData_IntoDb.Data.JsAlert.Alert("数据库读取错误"); 
                }
            }
            catch
            {
                StructuredData_IntoDb.Data.JsAlert.Alert( "数据库读取错误");
            }
            finally
            {
                dt.Dispose();
            }

        }
        /// <summary>
        /// 初始化树数据
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="tvw">treeview</param>
        private void InitTree(DataTable dt, TreeView tvw)
        {
            TreeNode rootNode = new TreeNode("基础数据表");
            rootNode.ImageUrl = "leftimage/fopen.gif";
            rootNode.Expanded = false;
            DataView dv = new DataView(dt);
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                //node.Text = drv["table_name"].ToString() + "(" + drv["table_code"].ToString() + ")";
                //node.Value = drv["table_code"].ToString();
                node.Text = drv["TABLE_NAME"].ToString() + "(" + drv["TABLE_CODE"].ToString() + ")";
                node.Value = drv["TABLE_CODE"].ToString();
                node.ImageUrl = "leftimage/fclose.gif";
                node.NavigateUrl = "BasicData.aspx?table_code=" + Server.UrlEncode(node.Value) + "&AUTOCOLUMN=ID&table_name=" + Server.UrlEncode(node.Text);
                node.Target = "treeMainFrame";
                node.SelectAction = TreeNodeSelectAction.Expand;
                node.Expanded = false;
              
                rootNode.ChildNodes.Add(node);
            }
            tvw.Nodes.Add(rootNode);
            tvw.ExpandAll();
        }
      
    }
}